package org.xxpay.pay.ctrl.division;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.domain.api.ApiBuilder;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.pay.channel.DivisionNotifyInterface;
import org.xxpay.pay.util.SpringUtil;

import javax.servlet.http.HttpServletRequest;

@Controller
public class DivisionNotifyController {

	private DivisionNotifyInterface divisionNotifyInterface;

	private static final MyLog _log = MyLog.getLog(DivisionNotifyController.class);

	@RequestMapping("/notify/{channel}/division_notify.htm")
	@ResponseBody
	public String notify(HttpServletRequest request, @PathVariable("channel") String channel){

		_log.info("====== 开始接收{}分账回调通知 ======", channel);
		try {
			divisionNotifyInterface = (DivisionNotifyInterface) SpringUtil.getBean(channel.toLowerCase() +  "DivisionNotifyService");
		}catch (BeansException e) {
			_log.error(e, "");
			return ApiBuilder.bizError("分账渠道类型[channel="+channel+"]实例化异常").toJSONString();
		}
		JSONObject retObj = divisionNotifyInterface.doNotify(request);
		String notifyRes = retObj.getString(PayConstant.RESPONSE_RESULT);
		_log.info("响应给{}:{}", channel, notifyRes);
		_log.info("====== 完成接收{}分账回调通知 ======", channel);
		return notifyRes;
	}
}
